package com.web.graduationpro.manager.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.web.graduationpro.dao.service.ArticleTypeService;
import com.web.graduationpro.dao.service.GoodsTypeService;
import com.web.graduationpro.dao.service.InstrumentSchoolService;
import com.web.graduationpro.model.ArticleType;
import com.web.graduationpro.model.GoodsType;
import com.web.graduationpro.model.InstrumentSchool;

@Controller
@RequestMapping("/manager/common/*")
public class ManagerCommonController {
	
	@Resource(name="instrumentSchoolServiceImpl")
	private InstrumentSchoolService instrumentSchoolServiceImpl;
	
	@Resource(name="goodsTypeServiceImpl")
	private GoodsTypeService goodsTypeServiceImpl;

	@Resource(name="articleTypeServiceImpl")
	private ArticleTypeService articleTypeServiceImpl;
	
	@RequestMapping(value = "getInstrumentSchools", method = RequestMethod.POST)  
    public void  getInstrumentSchools(HttpServletRequest request,HttpServletResponse reponse) { 
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "";
        List<InstrumentSchool> datas = instrumentSchoolServiceImpl.findAll(InstrumentSchool.class);
		if(datas!=null && datas.size()!=0){
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(new String[]{"instrumentCourses","teachers"});
			JSONArray json = JSONArray.fromObject(datas,jc);
			result = json.toString();
		}
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "getGoodsTypes", method = RequestMethod.POST)  
    public void  getGoodsTypes(HttpServletRequest request,HttpServletResponse reponse) { 
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "";
        List<GoodsType> datas = goodsTypeServiceImpl.findAll(GoodsType.class);
		if(datas!=null && datas.size()!=0){
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(new String[]{"goods"});
			JSONArray json = JSONArray.fromObject(datas,jc);
			result = json.toString();
		}
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "getArticleTypes", method = RequestMethod.POST)  
    public void  getArticleTypes(HttpServletRequest request,HttpServletResponse reponse) { 
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "";
        List<ArticleType> datas = articleTypeServiceImpl.findAll(ArticleType.class);
		if(datas!=null && datas.size()!=0){
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(new String[]{"articles"});
			JSONArray json = JSONArray.fromObject(datas,jc);
			result = json.toString();
		}
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
